#include <bits/stdc++.h>
#define ll long long
#define str string
#define N 100003
using namespace std;

int a[N];
ll aa[N];
pair<int,int> q[N];
vector<int> f[N];
ll lb[20][N*2];

int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=m;i++){
        scanf("%d",&q[i].first);
        q[i].second=i;
    }
    sort(q+1,q+1+m);
    int sz=ceil(log2(n));
    for(int i=1;i<=sz;i++){
        f[i].resize(1<<(sz-i));
        for(int &j:f[i]) scanf("%1d",&j);
    }
    for(int ii=sz+1,i=1;ii>0;ii--,i*=2){
        for(int j=1;j<=i;j++){
            if(ii<=sz && f[ii][(j-1)/2]!=j%2) lb[ii][j]=1e15;
            else lb[ii][j]=max(lb[ii+1][(j-1)/2+1],1LL*ii);
        }
    }
    /*for(int i=1;i<=sz+1;i++){
        for(int j=1;j<=(1<<(sz-i+1));j++){
            printf("%lld\t",lb[i][j]%100);
        }
        puts("");
    }*/
    int t;
    scanf("%d",&t);
    while(t--){
        int x[4];
        for(int i=0;i<4;i++) scanf("%d",&x[i]);
        for(int i=1;i<=(1<<sz);i++) aa[i]=(i<=n?a[i]^x[i%4]:1e15);
        ll rr=0;
        for(int i=1;i<=m;i++){
            ll r=0;
            for(int j=1;j<=q[i].first;j++){
                int ssz=ceil(log2(q[i].first));
                if(aa[j]>=lb[sz][j]){
                    // printf("%d,",j);
                    r+=j;
                }
            }
            puts("");
            rr^=r*q[i].second;
        }
        printf("%lld\n",rr);
    }
    return 0;
}



